package com.yilin.tms.data.controller;

import com.google.gson.reflect.TypeToken;
import com.yilin.commons.util.ArrayUtil;
import com.yilin.commons.util.CommonUtil;
import com.yilin.commons.util.JsonUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.Explain;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.SkipLoginVerify;
import com.yilin.tms.core.commons.annotation.UserTarget;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.data.monitor.TmsDataSync;
import com.yilin.tms.core.entity.data.monitor.TmsRootData;
import com.yilin.tms.core.entity.data.monitor.wlhy.TmsYD1001;
import com.yilin.tms.core.entity.truck.TravelLicense;
import com.yilin.tms.core.entity.user.UserType;

import com.yilin.tms.core.remote.IGpsRemote;
import com.yilin.tms.core.remote.IVehicleRemote;
import com.yilin.tms.data.service.ITmsDataService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 省运管数据管理
 */
@RestController
@RequestMapping("/data/tms/")
public class TmsDataController extends BaseController {

    @Resource
    ITmsDataService tmsDataService;
    @Resource
    IGpsRemote gpsRemote;
    @Resource
    IVehicleRemote vehicleRemote;

    @PostMapping(value = "getTmsRootPage")
    @NotNull({"page", "limit"})
    public ReturnData<Object> getTmsRootPage(TmsRootData tmsRootData, @Explain("页码") Integer page, @Explain("条数") Integer limit) {
        PageData<TmsRootData> tmsRootPage = tmsDataService.getTmsRootPage(tmsRootData, page, limit);
        return jsonView(SUCCESS_CODE, "获取成功", tmsRootPage);
    }

    /**
     * 拷贝数据
     * */
    @PostMapping(value = "copyTmsRootData")
    @UserTarget(UserType.platformUser)
    public ReturnData<List<TmsRootData>> copyTmsRootData() {
        List<TmsRootData> tmsRootList = tmsDataService.copyTmsRootData(getLoginUser().getOrganizeId());
        return jsonView(SUCCESS_CODE, "获取成功", tmsRootList);
    }

    /**
     * 同步数据状态
     * */
    @PostMapping(value = "syncTmsRootData")
    @NotNull({"ids", "status"})
    @UserTarget(UserType.platformUser)
    public ReturnData<Object> syncTmsRootData(String[] ids, TmsDataSync.SyncStatus status) {
        String platId = getLoginUser().getOrganizeId();
        String platName = getLoginUser().getOrganizeName();
        tmsDataService.syncTmsRootData(platId,platName, ArrayUtil.initList(ids), status);
        return jsonView(SUCCESS_CODE, "获取成功");
    }


    /***
     * 修复数据
     * http://ip:10002/data/tms/repairTmsData
     * */
    @PostMapping(value = "repairTmsData")
    @SkipLoginVerify
    public ReturnData<List<TmsRootData>> repairTmsData() {
        TmsRootData query = new  TmsRootData();
        query.setQuery("ipcType=WLHY_YD1001");
        PageData<TmsRootData> tmsRootPage = tmsDataService.getTmsRootPage(query, 1, 9999);
        for (TmsRootData tmsRootData : tmsRootPage.getList()) {
            if(!tmsRootData.getIpcType().equals("WLHY_YD1001")) continue;
            String jsonContent = tmsRootData.getJsonContent();
            TmsYD1001 tmsYD1001 = JsonUtil.jsonToObject(jsonContent,new TypeToken<TmsYD1001>(){});

            double goodsItemGrossWeight = Double.parseDouble(tmsYD1001.getVehicleInfo().get(0).getGoodsInfo().get(0).getGoodsItemGrossWeight());
            tmsYD1001.getVehicleInfo().get(0).getGoodsInfo().get(0).setGoodsItemGrossWeight(CommonUtil.formatDoubleThree(goodsItemGrossWeight / 1000));


//            //行驶证,用于获取平台和车籍地
//            TravelLicense travelLicense = ParamUtil.isSuccess(vehicleRemote.getTravelLicenseByVehicleNo(tmsYD1001.getVehicleInfo().get(0).getVehicleNumber()));
//
//            //修复1：车籍地编码缺失
//            if (tmsYD1001.getActualCarrierInfo().getActualCarrierBusinessLicense()==null || tmsYD1001.getActualCarrierInfo().getActualCarrierBusinessLicense().length()<12){
//                //从高德获取区域码
//                String adCode = ParamUtil.isSuccess(gpsRemote.getAdCodeByAddress(travelLicense.getPlatId(), travelLicense.getAddress()));
//                tmsYD1001.getActualCarrierInfo().setActualCarrierBusinessLicense(String.format("%s000000", adCode));
//            }
//
//            //修复2：发货区域码缺失
//            if (tmsYD1001.getConsignorInfo().getCountrySubdivisionCode()==null){
//                //从高德获取区域码
//                String adCode = ParamUtil.isSuccess(gpsRemote.getAdCodeByAddress(travelLicense.getPlatId(), tmsYD1001.getConsignorInfo().getPlaceOfLoading()));
//                tmsYD1001.getConsignorInfo().setCountrySubdivisionCode(adCode);
//            }
//
//            //修复3：收货区域码缺失
//            if (tmsYD1001.getConsigneeInfo().getCountrySubdivisionCode()==null){
//                //从高德获取区域码
//                String adCode = ParamUtil.isSuccess(gpsRemote.getAdCodeByAddress(travelLicense.getPlatId(), tmsYD1001.getConsigneeInfo().getGoodsReceiptPlace()));
//                tmsYD1001.getConsigneeInfo().setCountrySubdivisionCode(adCode);
//            }

            //修复完成，写入数据
            tmsRootData.setJsonContent(JsonUtil.objectToJson(tmsYD1001));

            tmsDataService.updateById(tmsRootData);
        }

        return jsonView(SUCCESS_CODE, "修复成功");
    }



}
